/**
 * The MIT License
 *
 * Copyright (c) 2010 Benjamin Klum
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.helgoboss.obr_facade_ref_impl

import org.osgi.service.obr.RepositoryAdmin
import org.eclipse.scalamodules._
import org.osgi.service.packageadmin.PackageAdmin
import org.helgoboss.scala_logging_osgi.OsgiLogging
import org.osgi.framework.{BundleActivator, BundleContext}
import org.helgoboss.obr_facade.ObrFacade

class ObrFacadeRefImpl extends ObrFacade with BundleActivator with OsgiLogging {
    
    protected var bundleContext: BundleContext = _
    
    def start(context: BundleContext) {
        bundleContext = context
        context createService (this, Map.empty, interface[ObrFacade])
    }
    
    def stop(context: BundleContext) {
    }
    
    
    def installResource(symbolicName: String) {
        bundleContext findService withInterface[RepositoryAdmin] andApply { admin =>
            val resolver = admin.resolver
            val filterString = "(symbolicname=" + symbolicName + ")"
            val resources = admin.discoverResources(filterString)
            if (resources != null && resources.size > 0) {
                resolver.add(resources(0))
                if (resolver.resolve) {
                    log.debug("Successfully resolved [" + filterString + "]. Deploy now.")
                    resolver.deploy(true);
                } else {
                    log.debug("Could not resolve [" + filterString + "]")
                }
            } else {
                log.debug("Could not discover [" + filterString + "]")
            }
        }
    }
    
    def uninstallResource(symbolicName: String) {
        bundleContext findService withInterface[PackageAdmin] andApply { admin =>
            val bundles = admin.getBundles(symbolicName, null)
            if (bundles != null) {
                for(bundle <- bundles) {
                    bundle.uninstall
                }
            }
        }
    }
}   